package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.QuizBean
import com.horoscope.astrology.zodiac.signs.free.bean.QuizResultBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.*
import kotlinx.android.synthetic.main.activity_quiz_start.*

class QuizStartActivity : Base2Activity() {

    private var mBean = QuizBean()
    private var quizUrl = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        immersionWhite()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_quiz_start)

        /**
         * 1、从Quizzes List页面跳转
         * 2、接受Push 跳转
         */

        val url = intent.getStringExtra("quiz_url")
        if (url.isNullOrEmpty()) {
            showLoaded()
            val bean: QuizBean? = intent.getSerializableExtra("quizBean") as QuizBean?
            if (bean != null) mBean = bean
            initView()
        } else {
            FlurryEventUtil.putDataToEvent(
                EventType.TYPE_APP_OPEND, "type",
                SplashActivity.PUSH_QUIZ
            )
            showLoading()
            quizUrl = url
            getRequest()
        }

        activityShowAd("Interstitial_Quiz")
    }

    private fun getRequest() {
        val quizId = quizUrl.split("/")
        NetworkEngine.getQuizForPushUrl("quizid", quizId[quizId.size - 2] + "/",
            object : NetworkListener<QuizResultBean>() {
                override fun onSuccess(t: QuizResultBean) {
                    if (t.data.size > 0) {
                        mBean = t.data[0]
                        initView()
                    }
                    showLoaded()
                }

                override fun onFailed(t: QuizResultBean) {
                    super.onFailed(t)
                    if (t.data.size > 0) {
                        mBean = t.data[0]
                        initView()
                    }
                    showLoaded()
                }

                override fun onError(e: String?) {
                    super.onError(e)
                    showLoaded()
                    ToastUtil.showRewardError(this@QuizStartActivity)
                }
            })
    }

    fun initView() {
        ImageLoader.loadImage(iv_head, mBean.image)
        findViewById<TextView>(R.id.tv_title).text = mBean.title
        findViewById<TextView>(R.id.tv_sub_title).text = mBean.subtitle
        text_view_start?.setOnClickListener {
            if (SysUtils.isFastClick()) return@setOnClickListener
            startActivity(QuizActivity.generateIntent(this, mBean))
        }
        iv_back?.setOnClickListener {
            clickBack()
        }
    }

    private fun clickBack() {
        if (intent.getBooleanExtra("push", false)) {
            val mainIntent = Intent(this, MainActivity::class.java)
            mainIntent.putExtra("page", 1)
            val quizzesIntent = Intent(this, QuizzesActivity::class.java)
            startActivities(arrayOf(mainIntent, quizzesIntent))
        }
        finish()
    }

    override fun onBackPressed() {
        super.onBackPressed()
        clickBack()
    }

    companion object {
        fun generateIntent(context: Context, quizBean: QuizBean): Intent {
            val intent = Intent(context, QuizStartActivity::class.java)
            intent.putExtra("quizBean", quizBean)
            return intent
        }
    }
}